package com.hp.bon.sgw.domain;

import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.hp.bon.sgw.Constants;

/**
 * DCC message Translation Record
 * 
 * @author wanghan
 * 
 */
@Entity
@Table(name = "T_MSG_TRANS_RECORD")
public class MsgTransRecord implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8298837057611494387L;

	public static final String OPTYPE_SEND = "send";
	public static final String OPTYPE_RECV = "recv";

	public static final String MSG_TYPE_CCR = "CCR";
	public static final String MSG_TYPE_CCA = "CCA";

	public static final String MSG_TYPE_RAR = "RAR";
	public static final String MSG_TYPE_RAA = "RAA";
	public static final String MSG_TYPE_ASR = "ASR";
	public static final String MSG_TYPE_ASA = "ASA";

	public static final String MSG_TYPE_WSR = "WSR";
	public static final String MSG_TYPE_WSA = "WSA";
	public static final String MSG_TYPE_SCR = "SCR";
	public static final String MSG_TYPE_SCA = "SCA";

	private int id;
	private String businessCallId;
	/**
	 * dcc session id
	 */
	private String sessionID;

	private String serviceContextId;

	private String destRealm;
	private String destHost;

	private String originHost;

	private int channelId;
	private int nodeId;

	private int sendNodeId;
	private int receiveNodeId;
	/**
	 * CCR,CCA,WSR,WSA,SCR,SCA
	 */
	private String msgType;

	private Timestamp msgTime;

	private long msgTimeMillis;

	/**
	 * dcc消息长度
	 */
	private int msgLength;
	/**
	 * 消息的附加说明，
	 */
	private String mark;

	/**
	 * send,recv
	 */
	private String opType;

	/**
	 * dcc 消息结果码
	 */
	private int resultCode = -1;
	private int priority;
	private String msgDetail;
	private Integer validateCode;
	private String validateDesc;
	private Timestamp validateTime;

	private Integer ccRequestNumber;
	private String forwardInfo;
	private String routeRecord;
	/**
	 * 业务返回码
	 */
	private int serviceResultCode = -1;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public static String getMsgTypeByGateWay(int gatewayCode, boolean req) {
		switch (gatewayCode) {
		case Constants.SGW_GATEWAY_TYPE_DCC: {
			if (req) {
				return MSG_TYPE_CCR;
			} else {
				return MSG_TYPE_CCA;
			}
		}
		case Constants.SGW_GATEWAY_TYPE_WS: {
			if (req) {
				return MSG_TYPE_WSR;
			} else {
				return MSG_TYPE_WSA;
			}
		}
		case Constants.SGW_GATEWAY_TYPE_SCSP: {
			if (req) {
				return MSG_TYPE_SCR;
			} else {
				return MSG_TYPE_SCA;
			}
		}
		default:
			return "UNKOWN";
		}
	}

	public String getSessionID() {
		return sessionID;
	}

	public void setSessionID(String sessionID) {
		this.sessionID = sessionID;
	}

	public String getDestRealm() {
		return destRealm;
	}

	public void setDestRealm(String destRealm) {
		this.destRealm = destRealm;
	}

	public String getOriginHost() {
		return originHost;
	}

	public void setOriginHost(String originHost) {
		this.originHost = originHost;
	}

	public String getMsgType() {
		return msgType;
	}

	public void setMsgType(String msgType) {
		this.msgType = msgType;
	}

	@Column(name = "MARK", nullable = true, length = 1024)
	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public Timestamp getMsgTime() {
		return msgTime;
	}

	public void setMsgTime(Timestamp msgTime) {
		this.msgTimeMillis = msgTime.getTime();
		this.msgTime = msgTime;
	}

	public String getServiceContextId() {
		return serviceContextId;
	}

	public void setServiceContextId(String serviceContextId) {
		this.serviceContextId = serviceContextId;
	}

	public int getSendNodeId() {
		return sendNodeId;
	}

	public void setSendNodeId(int sendNodeId) {
		this.sendNodeId = sendNodeId;
	}

	public int getReceiveNodeId() {
		return receiveNodeId;
	}

	public void setReceiveNodeId(int receiveNodeId) {
		this.receiveNodeId = receiveNodeId;
	}

	public int getMsgLength() {
		return msgLength;
	}

	public void setMsgLength(int msgLength) {
		this.msgLength = msgLength;
	}

	public int getResultCode() {
		return resultCode;
	}

	public void setResultCode(int dccResultCode) {
		this.resultCode = dccResultCode;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public String getOpType() {
		return opType;
	}

	public void setOpType(String opType) {
		this.opType = opType;
	}

	@Column(name = "msgDetail", nullable = true)
	public String getMsgDetail() {
		return msgDetail;
	}

	public void setMsgDetail(String msgDetail) {
		this.msgDetail = msgDetail;
	}

	public Integer getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(Integer validateCode) {
		this.validateCode = validateCode;
	}

	public String getValidateDesc() {
		return validateDesc;
	}

	public void setValidateDesc(String validateDesc) {
		this.validateDesc = validateDesc;
	}

	public Timestamp getValidateTime() {
		return validateTime;
	}

	public void setValidateTime(Timestamp validateTime) {
		this.validateTime = validateTime;
	}

	@Column(name = "CC_REQUEST_NUMBER", nullable = true)
	public Integer getCcRequestNumber() {
		return ccRequestNumber;
	}

	public void setCcRequestNumber(Integer ccRequestNumber) {
		this.ccRequestNumber = ccRequestNumber;
	}

	@Column(name = "FORWARD_INFO", nullable = true)
	public String getForwardInfo() {
		return forwardInfo;
	}

	public void setForwardInfo(String forwardInfo) {
		this.forwardInfo = forwardInfo;
	}

	@Column(name = "ROUTE_RECORD", nullable = true)
	public String getRouteRecord() {
		return routeRecord;
	}

	public void setRouteRecord(String routeRecord) {
		this.routeRecord = routeRecord;
	}

	@Column(name = "BUSINESS_CALL_ID", nullable = true)
	public String getBusinessCallId() {
		return businessCallId;
	}

	public void setBusinessCallId(String businessCallId) {
		this.businessCallId = businessCallId;
	}

	@Column(name = "MSG_TIME_MILLIS", nullable = true)
	public long getMsgTimeMillis() {
		return msgTimeMillis;
	}

	public void setMsgTimeMillis(long msgTimeMillis) {
		this.msgTimeMillis = msgTimeMillis;
	}

	@Column(name = "SERVICE_RESULT_CODE")
	public int getServiceResultCode() {
		return serviceResultCode;
	}

	public int getChannelId() {
		return channelId;
	}

	public int getNodeId() {
		return nodeId;
	}

	public void setChannelId(int channelId) {
		this.channelId = channelId;
	}

	public void setNodeId(int nodeId) {
		this.nodeId = nodeId;
	}

	public void setServiceResultCode(int serviceResultCode) {
		this.serviceResultCode = serviceResultCode;
	}

	public String getDestHost() {
		return destHost;
	}

	public void setDestHost(String destHost) {
		this.destHost = destHost;
	}

	@Transient
	private String messageUniqueID;

	@Transient
	public String getMessageUniqueID() {
		return messageUniqueID;
	}

	@Transient
	public void setMessageUniqueID(String messageUniqueID) {
		this.messageUniqueID = messageUniqueID;
	}

	// @Transient
	// public Message message;
	//
	// @Transient
	// public Message getMessage() {
	// return message;
	// }
	//
	// @Transient
	// public void setMessage(Message message) {
	// this.message = message;
	// }

	@Transient
	public String toString() {
		String str = this.getClass().getSimpleName() + "=" + this.businessCallId + "|" + this.sessionID + "|" + this.ccRequestNumber + "|" + this.serviceContextId + "|" + this.msgType + "|" + this.opType + "|" + this.channelId + "|" + this.nodeId + "|" + this.originHost + "|" + this.destHost + "|"
				+ this.destRealm + "|" + this.mark + "|" + this.msgLength + "|" + this.msgTimeMillis + "|" + this.serviceResultCode + "|" + this.priority + "|" + this.resultCode + "|" + this.forwardInfo + "|" + this.routeRecord + "|" + this.msgDetail;
		return str;
	}

	public static String info = "#businessCallId|sessionID|ccRequestNumber|serviceContextId|msgType|opType|sendNodeId|receiveNodeId|originHost|destHost|destRealm|mark|msgLength|msgTimeMillis|serviceResultCode|priority|resultCode|forwardInfo|routeRecord|msgDetail";

	@Transient
	public static MsgTransRecord convertToObject(String str) {
		MsgTransRecord mtr = null;
		String[] strs = str.split("\\|");
		if (strs.length == 20) {
			mtr = new MsgTransRecord();
			mtr.setBusinessCallId(strs[0]);
			mtr.setSessionID(strs[1]);
			mtr.setCcRequestNumber(Integer.valueOf(strs[2]));
		}
		return mtr;
	}
}
